package com.whu.tcx01.utils;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Shp2Db {
    public static void shp2db(String shpPath) throws Exception {
        File file = new File(shpPath);

        try {
            ShapefileDataStore store = new ShapefileDataStore(file.toURI().toURL());
            SimpleFeatureSource featureSource = store.getFeatureSource();
            SimpleFeatureCollection featureCollection = featureSource.getFeatures();
            SimpleFeatureIterator iterator = featureCollection.features();
            while (iterator.hasNext()){
                SimpleFeature feature = iterator.next();
                String geometryText = feature.getDefaultGeometry().toString();// wkt格式
//                System.out.println(geometryText);
                int direction = feature.getAttribute("Oneway").toString().equals("T")?2:3;
                String ToTailWkt = geometryText.substring(0, geometryText.length() - 1);
//                System.out.println(ToTailWkt);

                String[] strHead = ToTailWkt.split("\\(", 2);//限制分成两部分
                String[] strList = strHead[1].substring(1, strHead[1].length() - 1).split("\\), \\(");
                for (int i = 0; i < strList.length; i++) {
                    String item = strList[i].trim();
//                    System.out.println(item);
                    String[] items = item.split(",");
                    long startId= -1;
                    long endId = -1;
                    String last = null;
                    double lastX = 0;
                    double lastY = 0;
                    List<String> lines = new ArrayList<>();
                    double distance = 0;
                    for (int j = 0; j < items.length; j++) {
                        String jItem = items[j].trim();
                        String[] jItems = jItem.split(" ");
                        double x = Double.parseDouble(jItems[0]);
                        double y = Double.parseDouble(jItems[1]);
                        if(j==0){
                            startId = getNodeId(x, y);
                        }
                        if(j==items.length-1){
                            endId = getNodeId(x, y);
                        }
                        String current = ""+x+','+y;
                        if(last!=null){
                            String tmp = last+";"+current;
                            lines.add(tmp);
                            distance+=Transform.lonLatDistance(lastX, lastY, x, y);
                        }
                        last = current;
                        lastX = x;
                        lastY = y;
                    }

                    long roadid = CRUD.insertRoad(startId,endId,direction,distance);
                    for(String line:lines){
                        CRUD.insertLine(roadid,direction,line);
                    }
                }
//                List<Property> propertyList = new ArrayList<Property>(feature.getValue());
//                for(Property property:propertyList){
//                    System.out.println("属性名称：" + property.getName());
//                    System.out.println("属性值：" + property.getValue());
//                    System.out.println("属性类型：" + property.getType());
//                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static long getNodeId(double x, double y) throws Exception {
        int id = CRUD.selectNode(x, y);
        if(id!=-1){
            return id;
        }else{
            return CRUD.insertNode(x, y);
        }
    }
}